home *** CD-ROM | disk | FTP | other *** search
/ Workbench Design / WB Collection.iso / workbench werkzeuge / uhren & terminkalender / kalender / cgcalendar / cgcal.c < prev    next >
C/C++ Source or Header  |  1996-04-07  |  12KB  |  410 lines

  1.  
  2. /*
  3. **                  »»» CG Calendar v.03 (Preliminary) «««
  4. **
  5. **                Written by Craig G. Callan     August 1994
  6. */
  7.  
  8.  
  9. #include <clib/intuition_protos.h>
  10. #include <intuition/intuition.h>
  11. #include <clib/exec_protos.h>
  12. #include <graphics/text.h>
  13. #include <graphics/gfx.h>
  14. #include <exec/types.h>
  15. #include <string.h>
  16. #include <stdio.h>
  17. #include <time.h>
  18.  
  19. #define HEIGHT      15
  20. #define WIDTH       40
  21. #define WINHEIGHT   160
  22. #define WINWIDTH    315
  23.  
  24.     struct      IntuitionBase   *IntuitionBase;
  25.     struct      Window          *Window;
  26.     struct      NewWindow       NewWindow = {320 - (WINWIDTH + 100) / 2, 20,
  27.                                 WINWIDTH + 100, WINHEIGHT, 0, 1, CLOSEWINDOW |
  28.                                 INTUITICKS, WINDOWCLOSE | SMART_REFRESH |
  29.                                 ACTIVATE | WINDOWDRAG | WINDOWDEPTH |
  30.                                 NOCAREREFRESH, NULL, NULL,
  31.                                 "CG Calender v.03 (Preliminary)", NULL,
  32.                                 NULL, 440, 160, 440, 160, WBENCHSCREEN};
  33.     struct      EasyStruct      myES = {sizeof (struct EasyStruct), 0,
  34.                                 "CG Calendar Request",
  35.                                 "Do You Really Want to Quit?", "YES|NO"};
  36.     struct      TextExtent      ResultTextExtent;
  37.     struct      tm              *localTime;
  38.  
  39.     time_t                      timeDate;
  40.     char                        buf [80], c, datestring[80], mname [12], monthname [13][12], Tim [9], weekname [8][10], wkname [10], yr [4];
  41.     long                        timx [3];
  42.     int                         day, julian, leapflg, month, wkday, wkdayfirst, year;
  43.     int                         monthlen [13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  44.     int                         CheckQ, ht, Number, reCheck, QFlag, wt;
  45.  
  46.     void main (), BuildTimeString (), doIDCMP (), doScreen (), doText (), GetToday (), Refresh (), ShutDown (), StartUp ();
  47.  
  48. /*
  49. »»» Start Of Main Program ««««««««««««««««««««««««««««««««««««««««««««««««««««
  50. */
  51.  
  52. void main()
  53. {
  54.     CheckQ = 0; reCheck = 0; QFlag = 0;
  55.     StartUp ();
  56.  
  57.     GetToday ();
  58.     julian = FindJulian (day, month, year);
  59.     doScreen ();
  60.     while (QFlag == 0)
  61.     {
  62.         doIDCMP ();
  63.         if (CheckQ == 1)
  64.         {
  65.             CheckQ = 0;
  66. /*
  67.     »»» This part is for the "Do You Really Want to Quit?" requester «««
  68.  
  69.             QFlag = EasyRequest (NULL, &myES, NULL, NULL, Number);
  70. */
  71.             QFlag = 1;
  72.         }
  73.     }
  74.     ShutDown ();
  75.     exit (TRUE);
  76. }
  77.  
  78. /*
  79. »»» All Subroutines/Functions Reside Below This Point ««««««««««««««««««««««««
  80. */
  81.  
  82. void BuildTimeString ()
  83. {
  84.     USHORT                      Hours, Mins;
  85.  
  86.     Hours = (USHORT) timx [1] / 60;
  87.     Tim [5] = ' '; Tim [7] = 'M'; Tim [8] = 0;
  88.     Mins = (USHORT) timx [1] - (60 * Hours);
  89.     if (Hours > 12)
  90.     {
  91.         Hours = Hours - 12;
  92.         Tim [6] = 'P';
  93.     }
  94.     else
  95.         Tim [6] = 'A';
  96.     Tim [0] = (Hours < 10) ? '0' : (Hours / 10) + '0';
  97.     Tim [1] = (Hours < 10) ? '0' + Hours : (Hours % 10) + '0';
  98.     if (Tim [2] == ':')
  99.         Tim [2] = ' ';
  100.     else
  101.         Tim [2] = ':';
  102.     Tim [3] = (Mins < 10) ? '0' : (Mins / 10) + '0';
  103.     Tim [4] = (Mins < 10) ? '0' + Mins : (Mins % 10) + '0';
  104. }
  105.  
  106. int CheckForLeapYear (yr)
  107. {
  108.     if ((yr % 4) == 0)
  109.     {                     /* Check for leap year and alter February MONTHLEN */
  110.         monthlen [2] = 29;
  111.         return 1;
  112.     }
  113.     else
  114.     {
  115.         monthlen [2] = 28;
  116.         return 0;
  117.     }
  118. }
  119.  
  120. void doIDCMP ()
  121. {
  122.     struct      IntuiMessage    *message;
  123.     static      USHORT          ticks;
  124.     char                        Str [2];
  125.     int                         bDay, bMonth, bYear;
  126.  
  127.     WaitPort (Window -> UserPort);
  128.     while (message = (struct IntuiMessage *) GetMsg (Window -> UserPort))
  129.     {
  130.         switch (message -> Class)
  131.         {
  132.             case CLOSEWINDOW:
  133.                 CheckQ = 1;
  134.                 break;
  135.             case INTUITICKS:
  136.                 if (ticks++ > 7)
  137.                 {
  138.                     ticks = 0;
  139.                     DateStamp (timx);
  140.                     BuildTimeString (timx);
  141.                     doText (310, WINWIDTH + 86, 62 + ht, Tim, 1);
  142.                     reCheck++;
  143.                     if (reCheck > 1)           /* Recheck date every second */
  144.                     {
  145.                         reCheck = 0;
  146.                         bDay = day; bMonth = month; bYear = year;
  147.                         GetToday ();
  148.                         julian = FindJulian (day, month, year);
  149.                         if (day != bDay || month != bMonth || year != bYear)
  150.                             Refresh ();
  151.                     }
  152.                 }
  153.                 break;
  154.         }
  155.         ReplyMsg (message);
  156.     }
  157. }
  158.  
  159. void doScreen ()
  160. {
  161.     char        Day [4], buf [40];
  162.     int         c1, c2, d, i, j, w, x, y;
  163.  
  164. /*
  165. »»» Draw the "Info" part of the window «««
  166. */
  167.  
  168.     doText (310, WINWIDTH + 86, 58 - ht + (ht / 2), "Information", 2);
  169.     SetAPen (Window -> RPort, 1);
  170.     Move (Window -> RPort, 310, 58 + (HEIGHT * 6) + 6);
  171.     Draw (Window -> RPort, 310, 58);
  172.     Draw (Window -> RPort, WINWIDTH + 86, 58);
  173.     SetAPen (Window -> RPort, 2);
  174.     Draw (Window -> RPort, WINWIDTH + 86, 58 + (HEIGHT * 6) + 6);
  175.     Draw (Window -> RPort, 310,  58 + (HEIGHT * 6) + 6);
  176.     Move (Window -> RPort, (WINWIDTH + 86 + 310) / 2 - (wt / 2), 58 - ht + (ht / 2));
  177.     doText (310, WINWIDTH + 86, 62 + ht, "--:-- --", 1);
  178.     strcopy (buf, "Julian ");
  179.     for (i = 0; i < 4; i++)
  180.         Day [i] = 0;
  181.     itoa (julian, Day);
  182.     strcat (buf, Day);
  183.     doText (310, WINWIDTH + 86, 58 + (ht * 4), buf, 1);
  184.     if (leapflg == 1)
  185.         doText (310, WINWIDTH + 86, 62 + (ht * 5), "Leap Year", 1);
  186. /*
  187. »»» Draw Calendar «««
  188. */
  189.     d = 1; y = 58;
  190.     buf [0] = 0;
  191.     strcat (buf, monthname [month]); strcat (buf, " "); strcat (buf, yr);
  192.     doText (0, WINWIDTH, 30, buf, 2);
  193.     w = wt / i;
  194.     SetAPen (Window -> RPort, 1);
  195.     Day [0] = 0; Day [1] = 0; Day [2] = 0;
  196.     for (i = 1; i < 8; i++)
  197.     {
  198.         x = 9 + (i * (WIDTH + 2)) - WIDTH - 2;
  199.         strcopy (buf, weekname [i]); buf [3] = 0;
  200.         doText (x, x + WIDTH, y - ht + (ht / 2), buf, 1);
  201.     }
  202.     do
  203.     {
  204.         if (d == 1)
  205.             j = wkdayfirst;
  206.         else
  207.             j = 1;
  208.         while (j < 8 && d < monthlen [month] + 1)
  209.         {
  210.             x = 10 + (j * (WIDTH + 2)) - WIDTH - 2;
  211.             Move (Window -> RPort, x, y + HEIGHT);
  212.             if (d == day)
  213.             {
  214.                 c1 = 2; c2 = 1;
  215.             }
  216.             else
  217.             {
  218.                 c1 = 1; c2 = 2;
  219.             }
  220.             SetAPen (Window -> RPort, c2);
  221.             Draw (Window -> RPort, x, y);
  222.             Draw (Window -> RPort, x + WIDTH, y);
  223.             SetAPen (Window -> RPort, c1);
  224.             Draw (Window -> RPort, x + WIDTH, y + HEIGHT);
  225.             Draw (Window -> RPort, x, y + HEIGHT);
  226.             SetAPen (Window -> RPort, 1);
  227.             itoa (d, Day);
  228.             Move (Window -> RPort, x + (w / (w / 2)) , y + ht);
  229.             if (d < 10)
  230.                 Text (Window -> RPort, Day, 1);
  231.             else
  232.                 Text (Window -> RPort, Day, 2);
  233.             d++; j++; x = x + WIDTH + 2;
  234.         }
  235.         y = y + HEIGHT + 1;
  236.     } while (d < monthlen [month] + 1);
  237. }
  238.  
  239.  
  240. void doText (wx1, wx2, y, str, color)
  241.     char                        str [80];
  242.     int                         color, wx1, wx2, y;
  243. {
  244.     int                         x;
  245.  
  246.     TextExtent (Window -> RPort, str, strlen (str), &ResultTextExtent);
  247.     ht = ResultTextExtent.te_Height;
  248.     if (ht > HEIGHT - 2)
  249.     {
  250.         ShutDown ();
  251.         printf ("\nError!  Please use a font that is smaller than %d points...\n\n", HEIGHT - 1);
  252.         exit (FALSE);
  253.     }
  254.     wt = ResultTextExtent.te_Width;
  255.     x = ((wx2 + wx1) / 2) - (wt / 2);
  256.     SetAPen (Window -> RPort, color);
  257.     Move (Window -> RPort, x, y);
  258.     Text (Window -> RPort, str, strlen (str));
  259. }
  260.  
  261. int FindJulian (dy, mnth, year)
  262. {
  263.     int     jul, i;
  264.  
  265.     leapflg = CheckForLeapYear (year);
  266.     jul = dy;
  267.     for (i = 1; i < mnth; i++)
  268.         jul = jul + monthlen [i];
  269.     return jul;
  270. }
  271.  
  272. void GetToday ()
  273. {
  274.     int         i, j;
  275.  
  276.     timeDate = time (NULL);
  277.     localTime = localtime (&timeDate);
  278.     strftime (datestring, (size_t)80, "\"%A, %d %B %Y\"\n", localTime );
  279.     i = 0;
  280.     while (datestring [i] != 'y' && datestring [i] != 0)
  281.     {
  282.         wkname [i] = datestring [i + 1];
  283.         i++;
  284.         wkname [i] = 0;
  285.     }
  286.     wkday = 0;
  287.     for (j = 1; j < 8; j++)
  288.         if (strcmp (weekname [j], wkname) == 0) wkday = j;
  289.     i = i + 3; j = 0;
  290.     while (datestring [i] != ' ' && datestring [i] != 0)
  291.     {
  292.         buf [j] = datestring [i];
  293.         i++; j++;
  294.         buf [j] = 0;
  295.     }
  296.     day = atoi (buf);
  297.     i++; j = 0;
  298.     while (datestring [i] != ' ' && datestring [i] != 0)
  299.     {
  300.         mname [j] = datestring [i];
  301.         i++; j++;
  302.         mname [j] = 0;
  303.     }
  304.     month = 0;
  305.     for (j = 1; j < 13; j++)
  306.         if (strcmp (monthname [j], mname) == 0) month = j;
  307.     i++; j = 0;
  308.     while (j < 4)
  309.     {
  310.         yr [j] = datestring [i];
  311.         i++; j++;
  312.         yr [j] = 0;
  313.     }
  314.     year = atoi (yr);
  315.     i = day; wkdayfirst = wkday;
  316.     while (i > 1)
  317.     {
  318.         i--; wkdayfirst--;
  319.         if (wkdayfirst < 1) wkdayfirst = 7;
  320.     }
  321.     leapflg = CheckForLeapYear (year);
  322. }
  323.  
  324. itoa (n, s)     register    int    n;
  325.                 register    char   *s;
  326. {
  327.     int         i, forechar;
  328.  
  329.     i = forechar = 0;
  330.     if (n < 0)
  331.     {
  332.         forechar = 1;
  333.         n = -n;
  334.     }
  335.     do
  336.     {
  337.         s [i++] = n % 10 + '0';
  338.     } while ((n /= 10) > 0);
  339.     if (forechar)
  340.         s [i++] = '-';
  341.     reverse (s);
  342. }
  343.  
  344. void Refresh ()
  345. {
  346.     SetAPen (Window -> RPort, 0);
  347.     RectFill (Window -> RPort, 4, ht * 3, WINWIDTH + 88, WINHEIGHT - 4);
  348.     doScreen ();
  349. }
  350.  
  351. reverse (s)     register    char    *s;
  352. {
  353.     register    int c, i, j;
  354.  
  355.     for (i = 0, j = strlen (s) - 1; i < j; i++, j--)
  356.     {
  357.         c = s [i];
  358.         s [i] = s [j];
  359.         s [j] = c;
  360.     }
  361. }
  362.  
  363. void ShutDown ()
  364. {
  365.     if (Window) CloseWindow (Window);
  366.     if (IntuitionBase) CloseLibrary (IntuitionBase);
  367. }
  368.  
  369. void StartUp ()
  370. {
  371.     int         i, j;
  372.     char        buf [4];
  373.  
  374.     IntuitionBase = (struct IntuitionBase *) OpenLibrary ("intuition.library", 37);
  375.     if (IntuitionBase == NULL)
  376.     {
  377.         printf ("    You must have Kickstart v37 or newer, aborting...\n");
  378.         exit (FALSE);
  379.     }
  380.     if ((Window = (struct Window *) OpenWindow (&NewWindow)) == NULL)
  381.     {
  382.         CloseLibrary (IntuitionBase);
  383.         exit (FALSE);
  384.     }
  385.     TextExtent (Window -> RPort, "Test", 4, &ResultTextExtent);
  386.     ht = ResultTextExtent.te_Height;
  387.     if (ht > HEIGHT - 2)
  388.     {
  389.         ShutDown ();
  390.         printf ("\nError!  Please use a font that is smaller than %d points...\n\n", HEIGHT - 2);
  391.         exit (FALSE);
  392.     }
  393.     strcopy (weekname [1], "Sunday"); strcopy (weekname [2], "Monday");
  394.     strcopy (weekname [3], "Tuesday"); strcopy (weekname [4], "Wednesday");
  395.     strcopy (weekname [5], "Thursday"); strcopy (weekname [6], "Friday");
  396.     strcopy (weekname [7], "Saturday");
  397.     strcopy (monthname [1], "January"); strcopy (monthname [2], "February");
  398.     strcopy (monthname [3], "March"); strcopy (monthname [4], "April");
  399.     strcopy (monthname [5], "May"); strcopy (monthname [6], "June");
  400.     strcopy (monthname [7], "July"); strcopy (monthname [8], "August");
  401.     strcopy (monthname [9], "September"); strcopy (monthname [10], "October");
  402.     strcopy (monthname [11], "November"); strcopy (monthname [12], "December");
  403. }
  404.  
  405. strcopy (t, s)  register    char    *t, *s;
  406. {
  407.     while (*t++ = *s++);
  408. }
  409.  
  410.